//
//  CustomerDetailCell.m
//  New_Runway
//
//  Created by NewLife on 2018/2/26.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "CustomerDetailCell.h"

@implementation CustomerDetailCell
{
    UIImageView *_carImageView;
    UILabel *_carNameLabel;
    UILabel *_carPriceLabel;
    UIButton *_deleteBtn;
}

- (void)setImageUrlStr:(NSString *)imageUrlStr {
    
    _imageUrlStr = imageUrlStr;
    
    [_carImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",ImageApi,imageUrlStr]] placeholderImage:[UIImage imageNamed:@"tu"]];
}

- (void)setCarNameStr:(NSString *)carNameStr {
    
    _carNameStr = carNameStr;
    
    _carNameLabel.text = _carNameStr;
    
    [self layoutUI];
}

- (void)setCarPriceStr:(NSString *)carPriceStr {
    
    _carPriceStr = carPriceStr;
    
    _carPriceLabel.text = carPriceStr;
}

- (void)setShowDeleteBtn:(BOOL)showDeleteBtn {
    
    _showDeleteBtn = showDeleteBtn;
    
    _deleteBtn.hidden = !showDeleteBtn;
    
    [self remakeConstraints];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self setupUI];
        
        [self layoutUI];
        
        self.separatorInset = UIEdgeInsetsZero;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)deleteBtnDidClick:(UIButton *)sender {
    
    if ([self.delegate respondsToSelector:@selector(customerDetailCell:deleteBtnClickWithIndex:)]) {
        [self.delegate customerDetailCell:self deleteBtnClickWithIndex:self.tag];
    }
}

- (void)setupUI {
    
    _carImageView = [[UIImageView alloc] init];
    _carImageView.backgroundColor = [UIColor whiteColor];
    _carImageView.opaque = YES;
    _carImageView.clipsToBounds = YES;
    _carImageView.contentMode = UIViewContentModeScaleAspectFill;

    _carNameLabel = [[UILabel alloc] init];
    _carNameLabel.font = kPingFangRegular12();
    _carNameLabel.textColor = [UIColor colorFromHexCode:@"#323232"];
    _carNameLabel.numberOfLines = 2;
    _carNameLabel.backgroundColor = [UIColor colorFromHexCode:@"#ffffff"];
    _carNameLabel.opaque = YES;
    
    _carPriceLabel = [[UILabel alloc] init];
    _carPriceLabel.font = kPingFangRegular13();
    _carPriceLabel.textColor = [UIColor colorFromHexCode:@"#ff6816"];
    _carPriceLabel.backgroundColor = [UIColor colorFromHexCode:@"#ffffff"];
    _carPriceLabel.opaque = YES;
    
    _deleteBtn = DPButton()
    .setTitleFont(kPingFangRegular13())
    .setNormalTitleColor(getColorFromString(@"323232"))
    .setNormalTitle(@"删除")
    .addClickAction(self, @selector(deleteBtnDidClick:));
    _deleteBtn.hidden = YES;
    
    [self.contentView addSubview:_carImageView];
    [self.contentView addSubview:_carNameLabel];
    [self.contentView addSubview:_carPriceLabel];
    [self.contentView addSubview:_deleteBtn];
}

- (void)layoutUI {
    
    [_carImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@90);
        make.height.equalTo(@60);
        make.left.equalTo(self.contentView.mas_left).offset(16);
        make.centerY.equalTo(self.contentView.mas_centerY);
    }];
    
    [_carNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_carImageView.mas_top);
        make.left.equalTo(_carImageView.mas_right).offset(10);
        make.right.equalTo(self.contentView.mas_right).offset(-20);
    }];
    
    [_carPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_carImageView.mas_bottom);
        make.left.equalTo(_carNameLabel.mas_left);
    }];
}

- (void)remakeConstraints {
    
    [_carNameLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_carImageView.mas_top);
        make.left.equalTo(_carImageView.mas_right).offset(10);
        make.right.equalTo(_deleteBtn.mas_left);
    }];
    
    [_deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView.mas_centerY);
        make.right.equalTo(self.contentView.mas_right).offset(-10);
        make.width.height.equalTo(@40);
    }];
}

@end
